package com.hotel.couponsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.status.ResultMsg;
import com.common.status.StatusCode;
import com.common.utils.ResultUtils;
import com.common.utils.ResultVo;
import com.hotel.couponsystem.pojo.CouponAdmin;
import com.hotel.couponsystem.pojo.vo.CouponAdminVo;
import com.hotel.couponsystem.service.CouponAdminService;
import com.hotel.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * (CouponAdmin)表控制层
 *
 * @author makejava
 * @since 2022-09-30 15:01:28
 */
@Api(value = "CouponAdminController",tags = {"优惠券登录模块"})
@RestController
@RequestMapping("/couponAdmin")
//@CrossOrigin
public class CouponAdminController {
    /**
     * 服务对象
     */


    private final CouponAdminService couponAdminService; // 优惠券用户业务借口
    private RedisTemplate redisTemplate; // redis组件
    @Autowired
    private RedisTemplate<String,String> redisTemplate1;

    public CouponAdminController(CouponAdminService couponAdminService,RedisTemplate redisTemplate) {
        this.couponAdminService = couponAdminService;
        this.redisTemplate = redisTemplate;
    }

    @ApiOperation(value = "查询所有用户")
    @GetMapping("/getUserList")
    public ResultVo<List<CouponAdmin>> getUserList() {
        List<CouponAdmin> list = couponAdminService.list();
        if (list.size() != 0) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,list);
        else return ResultUtils.error(ResultMsg.DATA_NULL, StatusCode.SYSTEM_ERROR_CODE,null);
    }

    @ApiOperation(value = "添加用户")
    @PostMapping("/insetUser")
    public ResultVo<CouponAdmin> insetUser(@RequestBody CouponAdmin couponAdmin) {
        boolean flag = couponAdminService.save(couponAdmin);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,flag);
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("/updateUser")
    public ResultVo<CouponAdmin> updateUser(@RequestBody CouponAdmin couponAdmin) {
        LambdaQueryWrapper<CouponAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CouponAdmin::getUserId,couponAdmin.getUserId());
        boolean flag = couponAdminService.update(queryWrapper);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,flag);
    }

    @ApiOperation(value = "注销用户")
    @DeleteMapping("/deleteUser")
    public ResultVo<CouponAdmin> deleteUser(@RequestBody CouponAdmin couponAdmin) {
        LambdaQueryWrapper<CouponAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CouponAdmin::getUserId,couponAdmin.getUserId());
        boolean flag = couponAdminService.remove(queryWrapper);
        if (flag) return ResultUtils.success(ResultMsg.SUCCESS_MSG, StatusCode.SUCCESS_CODE,flag);
        else return ResultUtils.error(ResultMsg.ERROR_MSG, StatusCode.SYSTEM_ERROR_CODE,flag);
    }


    @ApiOperation(value = "登录用户")
    @PostMapping("/loginCouponAdmin")
    public ResultVo<CouponAdminVo> loginCustomer(@RequestBody  CouponAdmin couponAdmin){
        CouponAdminVo adminVo = couponAdminService.login(couponAdmin);
        if (adminVo == null) return ResultUtils.error(ResultMsg.DATA_NULL,StatusCode.SYSTEM_ERROR_CODE);
        else return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,adminVo);
    }

    @ApiOperation(value = "注册用户")
    @PostMapping("/registerCouponAdmin")
    public ResultVo Register(@RequestBody  CouponAdmin couponAdmin){
        Boolean flag = couponAdminService.Register(couponAdmin);
        if (flag) return ResultUtils.success(ResultMsg.REGISTER_SUCCESS,StatusCode.SUCCESS_CODE);
        else return ResultUtils.error(ResultMsg.REGISTER_FAIL,StatusCode.SYSTEM_ERROR_CODE);
    }

    //    发送短信
    @ApiOperation(value = "发送短信")
    @PostMapping("/sendMessage")
    public ResultVo sendMessage(@RequestParam String userTelno){
//        1.根据手机号 从Redis查找code
        String code= (String) redisTemplate.opsForValue().get(userTelno);
//       如果是没有
        if(Strings.isEmpty(code)) {
            //        2.如果Redis获取不到，进行阿里云发送
            //        生成随机值，传递阿里云进行发送
            code = RandomUtil.getFourBitRandom();
            Map<String, Object> map = new HashMap<>();
            map.put("code", code);
            //        调用service发送短信的方法
            boolean isSend = couponAdminService.sendMessage(map, userTelno);
            if (isSend) {
                //            发送成功，班发送成功的验证码方到Redis里面
                //            设置有效时间
                redisTemplate.opsForValue().set(userTelno, code, 60, TimeUnit.SECONDS);
                //返回发送成功的提示
                return ResultUtils.error(ResultMsg.PHONE_CODE_FAIL, StatusCode.PHONE_CODE_SUCCESS);
            } else {
                //            return "短信发送失败";
                return ResultUtils.success(ResultMsg.PHONE_CODE_SUCCESS, StatusCode.PHONE_CODE_FAIL, code);
            }
        }
        // 若有 返回成功的提示
        return ResultUtils.error(ResultMsg.PHONE_CODE_FAIL, StatusCode.PHONE_CODE_SUCCESS);

    }

    //手机验证码登录
    @ApiOperation(value = "手机验证码登录")
    @PostMapping("/phoneByCode")
    public ResultVo phoneByCode(@RequestParam String userTelno,@RequestParam String code){
//        String token=couponAdminService.loginByCode(userTelno,code);
//        return token;
        CouponAdminVo couponAdminVo = couponAdminService.loginByCode(userTelno, code);
        if (couponAdminVo == null) return ResultUtils.error(ResultMsg.DATA_NULL,StatusCode.SYSTEM_ERROR_CODE);
        else return ResultUtils.success(ResultMsg.SUCCESS_MSG,StatusCode.SUCCESS_CODE,couponAdminVo);
    }
}

